package com.health.tools;

import java.text.SimpleDateFormat;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.viewscontrols.NovaApplication;
import com.health.db.FDbManager;
import com.health.model.UpDataReturn;
import com.health.tablebean.JTYSCustomerDoctorCont;
import com.health.tablebean.JTYSDoctorEvaluateBean;
import com.health.tablebean.JTYS_BASEINFO;
import com.health.tablebean.SBCL_DATA;
import com.health.tablebean.webservice.MeasurementData;

import android.text.TextUtils;
import android.util.Log;

/**
 * 解析数据上传
 * 
 * @author nova
 *
 */
public class JX_SJSC {
	private final static String TAG = "JX_SJSC";
	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static FDbManager fdbManager = NovaApplication.fdbmanger;

	/**
	 * 解析服务满意度评价
	 * 
	 * @author 王婷婷
	 * @date 2017年4月13日 上午9:31:11
	 */
	public static UpDataReturn jxFwmydData(String result) {
		UpDataReturn updata = null;
		int successCount = 0;
		try {
			JSONObject obj;

			obj = new JSONObject(result.toString());
			System.out.println("接口返回的" + obj);
			System.out.println("接口返回的" + obj.toString());
			if (obj.has("success")) {
				if (obj.getBoolean("success")) {
					if (obj.has("infor")) {

						JSONArray jsons = obj.getJSONArray("infor");
						int length = jsons.length();
						if (length > 0) {
							updata = new UpDataReturn();
							updata.setSize(length);
							for (int i = 0; i < length; i++) {
								JSONObject obj_i = jsons.getJSONObject(i);
								String id = obj_i.getString("id");
								String flag = obj_i.getString("type");
								if (flag.equals("0")) {
									// 0 成功 1 失败
									successCount++;
									Log.w(TAG, "tt" + successCount);
									JTYSDoctorEvaluateBean evaluate = NovaApplication.fdbmanger
											.findAllByWhere(JTYSDoctorEvaluateBean.class, "id='" + id + "'").get(0);
									evaluate.setSign("1");
									NovaApplication.fdbmanger.updata(evaluate, "id='" + id + "'");
								}
							}
							updata.setSuccessCount(successCount);
							updata.setFailCount(length - successCount);

						}

					}
				}
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return updata;

	}

	/**
	 * 解析数据上传
	 * 
	 * @param result
	 * @return
	 * @author 王婷婷
	 * @date 2017年6月6日 下午3:44:20
	 */
	public static UpDataReturn jxCJYHData(String result) {
		UpDataReturn updata = null;
		int successCount = 0;
		int failCountYcz = 0;
		String errorMsg = "";
		try {
			JSONObject obj;

			obj = new JSONObject(result.toString());
			System.out.println("接口返回的" + obj);
			System.out.println("接口返回的" + obj.toString());
			if (obj.has("success")) {
				if (obj.getBoolean("success")) {
					if (obj.has("infor")) {

						JSONArray jsons = obj.getJSONArray("infor");
						int length = jsons.length();
						if (length > 0) {
							updata = new UpDataReturn();
							updata.setSize(length);
							for (int i = 0; i < length; i++) {
								JSONObject obj_i = jsons.getJSONObject(i);
								String cardNum = obj_i.getString("cardNum");
								if (obj_i.getBoolean("uploadFlag")) {
									// 0 成功 1 失败
									successCount++;
									Log.w(TAG, "tt" + successCount);
									JTYS_BASEINFO evaluate = NovaApplication.fdbmanger
											.findAllByWhere(JTYS_BASEINFO.class, "cardNum='" + cardNum + "'").get(0);
									evaluate.setSign("1");
									NovaApplication.fdbmanger.updata(evaluate, "cardNum='" + cardNum + "'");
								} else {
									if (TextUtils.isEmpty(errorMsg)) {
										errorMsg = cardNum + "上传失败：";
										Log.e("ddddddddddd", errorMsg);
									}

									if (obj_i.has("errorMsg")) {
										if (obj_i.has("errorCode")) {
											if ("该用户已存在并更新！".equals(obj_i.getString("errorMsg"))
													&& "28".equals(obj_i.getString("errorCode"))) {
												failCountYcz++;
												JTYS_BASEINFO evaluate = NovaApplication.fdbmanger.findAllByWhere(
														JTYS_BASEINFO.class, "cardNum='" + cardNum + "'").get(0);
												evaluate.setSign("1");
												Log.e("ddddddddddd", "该用户已存在！");
												NovaApplication.fdbmanger.updata(evaluate, "cardNum='" + cardNum + "'");
											}
										} else {
											errorMsg = errorMsg + "\n" + cardNum + "上传失败：";
											errorMsg = errorMsg + obj_i.getString("errorMsg");
										}
									}

								}
							}
							updata.setSuccessCount(successCount);
							updata.setFailCountYcz(failCountYcz);
							updata.setFailCount(length - successCount - failCountYcz);
							updata.setErrMsg(errorMsg);

						}

					}
				}
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return updata;

	}

	/**
	 * 家庭签约医生用户
	 * 
	 * @author 王婷婷
	 * @date 2017年4月13日 上午9:31:11
	 */
	public static UpDataReturn jxJTQYYSData(String result) {
		UpDataReturn updata = null;
		int successCount = 0;
		try {
			JSONObject obj;

			obj = new JSONObject(result.toString());
			System.out.println("接口返回的" + obj);
			System.out.println("接口返回的" + obj.toString());
			if (obj.has("success")) {
				if (obj.getBoolean("success")) {
					if (obj.has("infor")) {

						JSONArray jsons = obj.getJSONArray("infor");
						int length = jsons.length();
						if (length > 0) {
							updata = new UpDataReturn();
							updata.setSize(length);
							String id = "";
							String htbh = "";
							for (int i = 0; i < length; i++) {
								JSONObject obj_i = jsons.getJSONObject(i);
								id = obj_i.getString("id");

								if (obj_i.has("uploadFlag")) {
									if (obj_i.getBoolean("uploadFlag")) {
										successCount++;
										List<JTYSCustomerDoctorCont> evaluates = NovaApplication.fdbmanger
												.findAllByWhere(JTYSCustomerDoctorCont.class, "id='" + id + "'");
										if (evaluates.size() > 0) {
											evaluates.get(0).setSign("1");
											if (obj_i.has("htbh")) {
												htbh = obj_i.getString("htbh");
												Log.w(TAG, "tt" + successCount);
												evaluates.get(0).setHtbh(htbh);
											}
											NovaApplication.fdbmanger.updata(evaluates.get(0), "id='" + id + "'");
										}

									} else {
										List<JTYSCustomerDoctorCont> evaluates = NovaApplication.fdbmanger
												.findAllByWhere(JTYSCustomerDoctorCont.class, "id='" + id + "'");

										if (evaluates.size() > 0) {
											evaluates.get(0).setSign("1");
											if (obj_i.has("htbh")) {
												htbh = obj_i.getString("htbh");
												Log.w(TAG, "tt" + successCount);
												evaluates.get(0).setHtbh(htbh);
											}
											NovaApplication.fdbmanger.updata(evaluates.get(0), "id='" + id + "'");
										}
									}
								}

							}
							updata.setSuccessCount(successCount);
							updata.setFailCount(length - successCount);

						}

					}
				}
			}

		} catch (

		JSONException e)

		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return updata;

	}

	/**
	 * 解析测量数据上传
	 * 
	 * @param result
	 * @return
	 */
	public static UpDataReturn jxMeasureDataHistoryData(String result) {
		List<Integer> listId = new MeasurementData().converData().getListId();
		UpDataReturn updata = null;
		int failCount = 0;
		try {
			JSONObject obj;
			obj = new JSONObject(result.toString());
			System.out.println("测量数据接口返回的" + obj);
			System.out.println("测量数据接口返回的" + obj.toString());
			if (obj.has("success")) {
				if (obj.getBoolean("success")) {
					if (obj.has("infor")) {
						JSONArray jsons = obj.getJSONArray("infor");
						int length = jsons.length();
						updata = new UpDataReturn();
						updata.setSize(listId.size());
						if (length > 0) {
							for (int i = 0; i < length; i++) {
								JSONObject obj_i = jsons.getJSONObject(i);
								String id = obj_i.getString("id");
								String flag = obj_i.getString("errorCode");
								if (flag.equals("30")) {
									// 上传成功失败的标识 0 成功 1 失败 30数据已存在 7参数不合法

									SBCL_DATA sbcl = NovaApplication.fdbmanger
											.findAllByWhere(SBCL_DATA.class, "id='" + id + "'").get(0);
									sbcl.setIS_UPDATE("N");
									NovaApplication.fdbmanger.updata(sbcl, "id='" + id + "'");
								} else if (flag.equals("1") || flag.equals("7")) {
									failCount++;
								}
								for (Integer integer : listId) {
									if (id.equals(integer)) {

									} else {
										SBCL_DATA sbcl = NovaApplication.fdbmanger
												.findAllByWhere(SBCL_DATA.class, "id='" + integer + "'").get(0);
										sbcl.setIS_UPDATE("N");
										NovaApplication.fdbmanger.updata(sbcl, "id='" + integer + "'");
									}
								}

							}

						} else {
							for (Integer integer : listId) {
								SBCL_DATA sbcl = NovaApplication.fdbmanger
										.findAllByWhere(SBCL_DATA.class, "id='" + integer + "'").get(0);
								sbcl.setIS_UPDATE("N");
								NovaApplication.fdbmanger.updata(sbcl, "id='" + integer + "'");
							}

						}
						updata.setSuccessCount(listId.size() - failCount);
						updata.setFailCount(failCount);

					}
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return updata;
	}
}
